/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sarco.bwi;

import com.sarco.bwi.lib.Blender;
import com.sarco.bwi.lib.Config;
import com.sarco.bwi.lib.Errors;
import com.sarco.bwi.lib.FileManagement;
import com.sarco.bwi.lib.Job;
import com.sarco.bwi.lib.Mysql;
import com.sarco.bwi.lib.OSInformation;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iktsup
 */
public class Main {

    public static void main(String[] args) {        
        Config config = FileManagement.loadConfig(Config.DEFAULT_CONFIG);
        Mysql mysql = FileManagement.loadMysql(config.getMysql_host(),
                config.getMysql_user(),
                config.getMysql_pass(),
                config.getMysql_db());
        Blender blender = null;
        try {
            // Fetching job data:
            Job[] jobs = mysql.getJobData();
            Job currentjob = null;

            // Finding the first one which isn't taken yet:
            for (Job j : jobs) {
                if (!j.isTaken() && !j.isAbortrequested() && currentjob == null) {
                    currentjob = j;
                }
            }

            // Inserting client information:
            if (currentjob != null) {
                Calendar calstart = Calendar.getInstance(Locale.ROOT);
                
                calstart.set(calstart.get(Calendar.YEAR),
                        calstart.get(Calendar.MONTH),
                        calstart.get(Calendar.DATE),
                        calstart.get(Calendar.HOUR_OF_DAY),
                        calstart.get(Calendar.MINUTE),
                        calstart.get(Calendar.SECOND));
                calstart.setTime(new Date(System.currentTimeMillis()));
                Calendar.getInstance(Locale.ROOT);
                String sstart = "" + calstart.getTime();
                
                currentjob.setTimestarted(calstart.getTime());
                currentjob.setClienthostname(OSInformation.getHostname());
                currentjob.setClientip(OSInformation.getIpAddress());
                currentjob.setClientos(OSInformation.getOsName() + " "
                        + OSInformation.getOsPatchLevel() + " "
                        + OSInformation.getSystemArchitecture() + "bit");
                mysql.updateJobEntry(currentjob);
                
                currentjob.setTaken(true);
                currentjob.setPercentdone(0);
                currentjob.setTimeleft(null);

                // Updating mysql:
                mysql.updateJobEntry(currentjob);

                // Close mysql connection:
                mysql.closeConnection();
                
                blender = new Blender(config.getBlenderPath(), config.getBlend(),
                        config.getOuput(), config.getFormat(), 1, mysql, config,
                        currentjob);
                blender.render();

                // Job finished, once again updating mysql database entry:            
                if (mysql.getStatement() == null) {
                    mysql.connect(config.getMysql_host(), config.getMysql_user(),
                            config.getMysql_pass(), config.getMysql_db());
                }
                
                jobs = mysql.getJobData();
                for (Job j : jobs) {
                    if (j.getIdjob() == currentjob.getIdjob()) {
                        currentjob = j;
                    }
                }
                
                Calendar calfinished = Calendar.getInstance();
                calfinished.set(calfinished.get(Calendar.YEAR),
                        calfinished.get(Calendar.MONTH),
                        calfinished.get(Calendar.DATE),
                        calfinished.get(Calendar.HOUR_OF_DAY),
                        calfinished.get(Calendar.MINUTE),
                        calfinished.get(Calendar.SECOND));
                currentjob.setTimefinished(calfinished.getTime());
                if (!currentjob.isAbortrequested()) {
                    currentjob.setPercentdone(100);
                }
                
                mysql.updateJobEntry(currentjob);
                mysql.closeConnection();
            } else {
                System.out.println("No jobs found, either all is taken or none is created.");
                System.exit(Errors.NO_JOBS);
            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
